<?php

class AppLog
{
	var $file;
	var $mod = "";
	var $do = "";
	var $timeStart = array();
	
	function __construct() 
	{
		$name = date("Ymd");
		$this->file = fopen(LOGS."/$name.log", "a+");
	}

	function __destruct() 
	{
		if ( $this->file ) fclose($this->file);
	}

	function __open( $fl = "" )
	{
		$name = date("Ymd");
		$this->file = fopen(LOGS."/$fl$name.log", "a+");
	}
	
	function __microtimeFloat()
	{
		list($usec, $sec) = explode(" ", microtime());
		return ((float)$usec + (float)$sec);
	}

	function logWrite($str, $level = 0)
	{
		if ( DEBUG_LEVEL >= $level )
		{
			$date = date("Y/m/d H:i:s");
			fwrite($this->file, "$date : ".$this->mod."/".$this->do." : $str\n");
		}
	}

	function dbgWrite($str, $level = 0)
	{
		if ( DEBUG_LEVEL >= $level )
		{
			$date = date("Y/m/d H:i:s");
			fwrite($this->file, "$date : DEBUG : $str\n");
		}
	}
	
	function startProcess( $id = "Main", $type = "" )
	{		
		$this->timeStart[ $id ] = $this->__microtimeFloat();
		if ( $type == "LOG" || $type == "ALL" ) $this->logWrite("Time execution $id start = ".$this->timeStart[ $id ]);
		if ( $type == "RET" || $type == "ALL" ) return $this->timeStart[ $id ];
	}
	
	function endProcess( $id = "Main", $type = "LOG" )
	{		
		$timeEnd = $this->__microtimeFloat();
		$time = $timeEnd - $this->timeStart[ $id ];
		if ( $type == "LOG" || $type == "ALL" ) $this->logWrite("Time execution $id = ".$time);
		if ( $type == "RET" || $type == "ALL" ) return $time;
	}	
}

?>